package com.datareport.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.datareport.data.dto.ResultDTO;
import com.datareport.data.entity.Role;
import com.datareport.data.entity.User;
import com.datareport.data.entity.UserRole;
import com.datareport.data.mapper.RoleMapper;
import com.datareport.data.mapper.UserMapper;
import com.datareport.data.mapper.UserRoleMapper;
import com.datareport.data.service.RoleService;
import com.datareport.data.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DataReport
 * @since 2021-11-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public User getByName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", username);

        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {

        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", "select role_id from user_role where user_id = " + userId);

        List<Role> roles = roleMapper.selectList(wrapper);

        //"ROLE_"是默认前缀
        String roleNames = roles.stream().map(r ->"ROLE_"+r.getCode()).collect(Collectors.joining(","));

        String authority = roleNames;

        return authority;
    }

    @Override
    public User selectSuperAdmin(Long RoleId) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.inSql("id", "select user_id from user_role where role_id=" + RoleId);

        User superUser = userMapper.selectOne(wrapper);

        return superUser;
    }

    @Override
    public List<User> listUserById(Long admin_id) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.notInSql("id", "select user_id from user_role where role_id=" + admin_id);

        List<User> users = userMapper.selectList(queryWrapper);

        return users;
    }

    @Override
    public List<User> listAdminUserById(Long superAdmin_id) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();

        queryWrapper.notInSql("id", "select user_id from user_role where role_id=" + superAdmin_id);

        List<User> users = userMapper.selectList(queryWrapper);

        return users;
    }

    @Override
    public List<User> listAdminById(Long adminId) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.inSql("id","select user_id from user_role where role_id="+adminId);

        List<User> users = userMapper.selectList(wrapper);

        return users;
    }

    @Override
    public ResultDTO deleteUser(Long id) {
        User deleteUser = userMapper.selectById(id);

        //查找管理员用户列表
        Role role = roleService.selectAdmin();

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.inSql("id","select user_id from user_role where role_id="+role.getId());

        //所有管理员用户
        List<User> users = userMapper.selectList(wrapper);

        //判断要删除的用户是不是管理员
        if(users.contains(deleteUser)){

            //删除权限
            userRoleMapper.deleteByUserId(id);

        }
        userMapper.deleteById(id);
        return ResultDTO.okOf();
    }
}
